/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.AppServerSupport;
import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CreateOrModifyBusContent1 extends Content {
	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private String domainId = null;
	private TextBox name, code, url;
	private TextArea desc;
	private String burl;
	private ServiceBus bus;
	private boolean isCreate;
	
	private ListBox appServer, version;


	public CreateOrModifyBusContent1(DomainMessages cons, String dId,
			boolean create, ServiceBus sca, String surl) {
		this.cons = cons;
		this.isCreate = create;
		this.domainId = dId;
		this.bus = sca;
		this.burl = surl;

		VerticalPanel panel = new VerticalPanel();
		panel.setSpacing(5);
		Grid table = new Grid(6, 2);
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		ff.setStyleName(3, 0, "tablecol");
		ff.setStyleName(4, 0, "tablecol");
		ff.setStyleName(5, 0, "tablecol");

		
		table.setHTML(0, 0, cons.name());
		table.setHTML(1, 0, cons.code());
		table.setHTML(2, 0, cons.appServer());
		table.setHTML(3, 0, cons.serVersion());
		table.setHTML(4, 0, cons.desc());
		table.setHTML(5, 0, cons.url());

		if (isCreate) {
			name = new TextBox();
			name.setVisibleLength(32);

			code = new TextBox();
			code.setVisibleLength(32);
			appServer = new ListBox();
			String[] names = AppServerSupport.getSupportServerNameList();
			for (String name : names) {
				appServer.addItem(name, AppServerSupport.getServerValue(name));
			}
			appServer.setItemSelected(0, true);

			version = new ListBox();
			ArrayList<String> v = AppServerSupport.getSupportVersion(appServer
					.getValue(0));
			for (String ver : v) {
				version.addItem(ver, ver);
			}
			version.setItemSelected(0, true);
			
			appServer.addChangeListener(new ChangeListener() {

				public void onChange(Widget sender) {
					ListBox t = (ListBox) sender;
					version.clear();
					ArrayList<String> v = AppServerSupport.getSupportVersion(t
							.getValue(t.getSelectedIndex()));
					for (String ver : v) {
						version.addItem(ver, ver);
					}
					version.setItemSelected(0, true);

				}

			});
			desc = new TextArea();
			desc.setCharacterWidth(64);
			desc.setVisibleLines(6);
			url = new TextBox();
			url.setVisibleLength(80);
			url.setText("http://[service bus server ip]:[port]");
		} else {
			name = new TextBox();
			name.setVisibleLength(32);
			name.setText(bus.getName());

			code = new TextBox();
			code.setVisibleLength(32);
			code.setText(bus.getCode());

			appServer = new ListBox();
			String[] names = AppServerSupport.getSupportServerNameList();
			for (int i = 0 , j = names.length ; i< j ; i ++) {
				appServer.addItem(names[i], AppServerSupport.getServerValue(names[i]));
				if(bus.getAppServer().equals(names[i])){
					appServer.setItemSelected(i, true);
				}
			}
			

			version = new ListBox();
			ArrayList<String> v = AppServerSupport.getSupportVersion(appServer
					.getValue(0));
			for (int i = 0, j = v.size() ; i < j ; i ++) {
				version.addItem(v.get(i), v.get(i));
				if(bus.getServerVersion().equals(v.get(i))){
					version.setItemSelected(i, true);
				}
			}
			
			


			appServer.addChangeListener(new ChangeListener() {

				public void onChange(Widget sender) {
					ListBox t = (ListBox) sender;
					version.clear();
					ArrayList<String> v = AppServerSupport.getSupportVersion(t
							.getValue(t.getSelectedIndex()));
					for (String ver : v) {
						version.addItem(ver, ver);
					}
					version.setItemSelected(0, true);
				}

			});
			desc = new TextArea();
			desc.setCharacterWidth(64);
			desc.setVisibleLines(6);
			desc.setText(bus.getDescription());

			url = new TextBox();
			url.setVisibleLength(80);
			url.setText(burl);

		}

		table.setWidget(0, 1, name);
		table.setWidget(1, 1, code);
		table.setWidget(2, 1, appServer);
		table.setWidget(3, 1, version);
		
		table.setWidget(4, 1, desc);
		table.setWidget(5, 1, url);

		Button b = new Button(cons.next());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget send) {
				if (!validate())
					return;
				if (isCreate) {
					bus = new ServiceBus();
					bus.setDomainId(domainId);

				}
				bus.setAppServer(appServer.getValue(appServer.getSelectedIndex()));
				bus.setServerVersion(version.getValue(version.getSelectedIndex()));
				
				bus.setCode(code.getText());
				bus.setDescription(desc.getText());
				bus.setName(name.getText());
				HashMap p = new HashMap();
				p.put("bus", bus);
				p.put("url", url.getText());
				if (!isCreate && !url.getText().trim().equals(burl)) {
					p.put("urlChanged", true);
				}
				if (isCreate) {
					listener.onContentChanged(
							DomainContentConstants.CREATE_BUS1,
							DomainContentConstants.CREATE_BUS2, p);
				} else {
					listener.onContentChanged(
							DomainContentConstants.MODIFY_BUS1,
							DomainContentConstants.MODIFY_BUS2, p);

				}
			}
		});

		panel.add(table);
		panel.add(b);
		initWidget(panel);

	}

	private boolean validate() {
		if (!StringUtils.checkLenght(name.getText(), 1, 128)) {
			Window.alert(cons.lengthError(cons.name(), "1", "128"));
			return false;
		}
		if (!StringUtils.checkLenght(desc.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.desc(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(code.getText(), 1, 32)) {
			Window.alert(cons.lengthError(cons.code(), "1", "32"));
			return false;

		}
		if (StringUtils.isEmpty(url.getText())) {
			Window.alert(cons.isNull(cons.url()));
			return false;

		}
		return true;

	}

}
